package cn.gcyl.module.biz.controller.app;

import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.module.biz.controller.app.DTO.PayOrderNotifyReqDTO;
import cn.gcyl.module.biz.dal.dataobject.SysPayOrderDO;
import cn.gcyl.module.biz.dal.mysql.SysPayOrderMapper;
import cn.gcyl.module.biz.service.conferenceregistrationinformation.ConferenceRegistrationInformationService;
import cn.gcyl.module.biz.service.memberorder.MemberOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;

import static cn.gcyl.framework.common.pojo.CommonResult.success;


@Tag(name = "APP - 支付回调")
@RestController
@RequestMapping("/callback")
@Slf4j
@Validated
public class OrderCallbackController {

    @Resource
    private ConferenceRegistrationInformationService conferenceRegistrationInformationService;
    @Resource
    private SysPayOrderMapper sysPayOrderMapper;
    @Resource
    private MemberOrderService memberOrderService;

    @PostMapping("/pay")
    @Operation(summary = "支付回调")
    @PermitAll
    public CommonResult<Boolean> updateDemoOrderPaid(@RequestBody PayOrderNotifyReqDTO notifyReqDTO) {
        SysPayOrderDO payOrderDO = sysPayOrderMapper.selectById(notifyReqDTO.getPayOrderId());
        log.info("执行订单支付回调");
        Boolean result = conferenceRegistrationInformationService.getConferenceRegistrationInformationByOrderNo(notifyReqDTO.getMerchantOrderId(), payOrderDO);
        return success(result);
    }

//    @PostMapping("/update-refunded")
//    @Operation(summary = "退款回调") // 由 pay-module 支付服务，进行回调，可见 PayNotifyJob
//    @PermitAll // 无需登录，安全由 PayDemoOrderService 内部校验实现
//    public CommonResult<Boolean> updateOrderRefunded(@RequestBody PayRefundNotifyReqDTO notifyReqDTO) {
//        log.info("退款回调");
//        return success(true);
//    }

    @PostMapping("/memberPay")
    @Operation(summary = "开通会员支付回调")
    @PermitAll
    public CommonResult<Boolean> memberPay(@RequestBody PayOrderNotifyReqDTO notifyReqDTO) {
        log.info(":"+notifyReqDTO.toString());
        SysPayOrderDO payOrderDO = sysPayOrderMapper.selectById(notifyReqDTO.getPayOrderId());
        log.info("执行开通会员订单支付回调");
        log.info("执行开通会员订单支付回调"+payOrderDO);
        Boolean result = memberOrderService.getMemberOrderByOrderNo(notifyReqDTO.getMerchantOrderId(), payOrderDO);
        return success(result);
    }
}
